// pages/owModify/owModify.js
const app = getApp();
const util = require('../../utils/util');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    /*  */
    userInfo_share:{},
    /*  */
    showEmptyList: false,
    /*  */
    infoData:null,
    class_id: "",
    /* 图片数据 */
    imageData:[],
    ow_images:[]
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.setData({
      userInfo_share: app.globalData.get_userInfo()
    });
    let eventChannel = this.getOpenerEventChannel();
    //
    if(app.globalData.fromEmployeeToOwModify = true){
      app.globalData.fromEmployeeToOwModify = false;
      eventChannel.on("fromEmployeeToOwModify", data=>{
        console.log(data);
        //
        wx.setNavigationBarTitle({
          title: `${data.name}`,
        });
        //
        this.setData({
          infoData:data
        });
        //
        wx.showLoading({
          title: '获取数据',
          mask:true
        })
        //
        app.globalData.getClassItemInfo.call(this,app.globalData.homePage_item_id).then(()=>{
          wx.hideLoading();
          console.log(this.data.imageData);
          //
          this.setData({
            ow_images:this.data.imageData.map(item=>item.image)
          });
          //列表为空
          if(!this.data.ow_images.length){
            this.setData({
              showEmptyList:true
            });
          }
          //
        }).catch(err=>{
          wx.hideLoading();
        })
      });
    }
  },
  /* 预览图片 */
  previewOwImgs(e){
    let { index } = e.currentTarget.dataset;
    wx.previewImage({
      urls: this.data.ow_images,
      current: this.data.ow_images[index]
    })
  },
  /* 修改官网要展示的数据 */
  updateClassItemInfo(args){
    return app.globalData.$http.call(this,{
      api:"/updateClassItemInfo",
      data:args
    }).then(res=>{
      console.log(res);
      if(res.data.code=='1'){
        return Promise.resolve();
      }else{
        return Promise.reject('修改失败');
      }
    }).catch(err=>{
      return Promise.reject('修改失败');
    })
  },
  /* 删除图片 */
  removeOwImg(e){
    let { index } = e.currentTarget.dataset;
    wx.showModal({
      title:"提示",
      content:"确定要删除该官网图片吗？",
      success:res=>{
        if(res.confirm){
          //
          let tmpArr = this.data.ow_images;
          tmpArr.splice(index,1);
          this.setData({
            ow_images:tmpArr
          });
          //
          if(!this.data.ow_images.length){
            this.setData({
              showEmptyList:true
            });
          }
          //
          let tmpIntroImgsObj = {};
          let tmpIntroImgs = [];
          this.data.ow_images.forEach(item=>{
            tmpIntroImgs.push({
              "image":item,
              "style":"margin-top:20px;"
            });
          });
          tmpIntroImgsObj.images = tmpIntroImgs;
          tmpIntroImgsObj.background_style = "background:#ffffff;"
          //
          let modifyArgs = {
            image:JSON.stringify(tmpIntroImgsObj),
            item_id:app.globalData.homePage_item_id,
            name:this.data.infoData.name
          };
          console.log(modifyArgs);
          //
          wx.showLoading({
            title: "删除图片",
            mask:true
          })
          //
          this.updateClassItemInfo(modifyArgs).then(()=>{
            wx.hideLoading();
            wx.showToast({
              title:"删除成功"
            });
          }).catch(err=>{
            wx.hideLoading();
            wx.showModal({
              title:"提示",
              content:`${err}`,
              showCancel:false
            });
          });
        }
      }
    })
  },
  /* 添加图片 */
  addOwImg(){
    this.uploadOne().then(data=>{
      wx.showLoading({
        title:"添加图片",
        mask:true
      });
      //
      let tmpArr = this.data.ow_images;
      tmpArr.push(data.substring(1,data.length-1));
      this.setData({
        ow_images:tmpArr
      });
      //
      if(this.data.ow_images.length){
        this.setData({
          showEmptyList:false
        });
      }
      //
      let tmpIntroImgsObj = {};
      let tmpIntroImgs = [];
      this.data.ow_images.forEach(item=>{
        tmpIntroImgs.push({
          "image":item,
          "style":"margin-top:20px;"
        });
      });
      tmpIntroImgsObj.images = tmpIntroImgs;
      tmpIntroImgsObj.background_style = "background:#ffffff;"
      //
      let modifyArgs = {
        image:JSON.stringify(tmpIntroImgsObj),
        item_id:app.globalData.homePage_item_id,
        name:this.data.infoData.name
      };
      console.log(modifyArgs);
      //return Promise.resolve();
      return this.updateClassItemInfo(modifyArgs)
    }).then(()=>{
      wx.hideLoading();
      wx.showToast({
        title:"添加成功"
      });
    }).catch(err=>{
      wx.hideLoading();
      wx.showModal({
        title:"提示",
        content:`${err}`,
        showCancel:false
      });
    })
  },
  /* 上传图片 */
  uploadOne(){
    return new Promise((resolve, reject)=>{
      wx.chooseImage({
        success:res => {
          wx.showLoading({
            title: '上传中...',
            mask:true
          });
          const tempFilePaths = res.tempFilePaths
          wx.uploadFile({
            url: `${app.globalData.Url}/uploadOne`, //
            filePath: tempFilePaths[0],
            name: 'file',
            formData: {},
            success: res=>{
              wx.hideLoading();
              console.log(res);
              const data = res.data
              //console.log(data);
              resolve(data)
            },
            fail:err=>{
              wx.hideLoading();
              reject("图片上传失败");
            }
          })
        },
        fail:err=>{
          wx.hideLoading();
          reject("选择要上传的图片出错");
        }
      });
    });
  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})